<template>
  <div style="display: flex">
    <img src="../assets/image/logo.png" height="70">
    <el-divider direction="vertical" style="height: 70px; vertical-align: top; border-color: black; border-width: 3px;"></el-divider>
    <img src="../assets/image/信息工程學院學生信息管理系統黑.png" height="60" style="vertical-align: top">
  </div>

  <el-menu
      router
      :default-active="$route.path"
      class="el-menu-demo"
      mode="horizontal"
  >
    <el-menu-item index="/home">首页</el-menu-item>
    <el-sub-menu index="2">
      <template #title>信息查询</template>
      <el-menu-item index="/message">学生信息查询</el-menu-item>
      <el-menu-item index="/awardMessage">奖惩信息查询</el-menu-item>
      <el-menu-item index="/grade">成绩查询</el-menu-item>
    </el-sub-menu>
    <el-sub-menu index="3">
      <template #title>个人中心</template>
      <el-menu-item @click="ChangePassword=true ;clearPwd;">修改密码</el-menu-item>
    </el-sub-menu>
    <el-sub-menu index="4">
      <template #title>上传数据</template>
      <el-menu-item index="/updateAward">上传奖项竞赛</el-menu-item>
    </el-sub-menu>
    <el-sub-menu index="5">
      <template #title>应用维护</template>
      <el-menu-item index="/instructor">辅导员</el-menu-item>
      <el-menu-item index="/unit">部门信息</el-menu-item>
    </el-sub-menu>
    <el-sub-menu index="6">
      <template #title>请假</template>
      <el-menu-item index="/instructor">请假申请</el-menu-item>
      <el-menu-item index="/unit">请假审批</el-menu-item>
    </el-sub-menu>
  </el-menu>
  <div>
    <el-dialog
        @close="goBack"
        v-model="ChangePassword"
        :close-on-click-modal="false"
        title="用户修改"
        width="700px">
      <!--    用户编辑-->
      <div>
        <el-form
            :model="twoPwd"
            ref="twoPwd"
            :rules="rules"
        >
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="twoPwd.oldPassword" placeholder="请输入"
                      style="position: relative ;left: 67px ; width:500px "></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="twoPwd.newPassword" placeholder="请输入"
                      style="position: relative ;left: 67px ; width:500px "></el-input>
          </el-form-item>
          <el-form-item label="再次输入新密码" prop="ReNewPassword">
            <el-input v-model="twoPwd.ReNewPassword" placeholder="请输入"
                      style="position: relative ;left: 13px ; width:500px "></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="revisePwd('twoPwd')">提交</el-button>
            <el-button @click="ChangePassword= false,goBack">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import request from "@/util/request.js";
import {ElMessage} from "element-plus";

export default {
  name: "Header",
  data() {
    return {
      ChangePassword: false,
      //新旧密码和学号
      twoPwd:{
        accountNo: "",
        oldPassword: "",
        newPassword: "",
        ReNewPassword:"",
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        ReNewPassword:[
          { required: true, message: '请再输入新密码', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
    console.log("初始化加载方法");
    this.AccountNo();
  },
  methods: {
    revisePwd(pwd){
      console.log(this.twoPwd)
      this.$refs[pwd].validate((valid) => {
        if(valid){
          if (this.twoPwd.newPassword != this.twoPwd.ReNewPassword) {
            ElMessage.error("两次新密码不一致")
            return
          }
          request.post("/account/revisePwd", this.twoPwd).then(res => {
            console.log(res.data)
            if (res.data.code == '400') {
              ElMessage.error("密码错误")
            } else if(res.data.code == '200'){
              ElMessage.success("修改成功")
                  this.ChangePassword= false
                  this.goBack()
            }
        })
        }else {
          console.log('error');
          return false;
        }
      })
  },
    AccountNo(){
      this.twoPwd.accountNo = JSON.parse(localStorage.getItem('user')).accountNo;
    },
    clearPwd(){
      this.twoPwd.newPassword = ""
      this.twoPwd.oldPassword = ""
      this.twoPwd.ReNewPassword = ""
    },
    goBack(){
      this.twoPwd.newPassword = ""
      this.twoPwd.oldPassword = ""
      this.twoPwd.ReNewPassword = ""
    },
  }
}
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.demonstration {
  display: block;
  color: #8492a6;
  font-size: 14px;
  margin-bottom: 20px;
}
</style>
